package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController("/wx/dz")
@RequestMapping("/wx/dz")
public class DzComtroller {

    @Autowired
    IAmdService iAmdService;

    @Autowired
    IAdzService iAdzService;

    @Autowired
    IAfjService iAfjService;

    @Autowired
    IAmjService iAmjService;

    @Autowired
    IAyyjlitemService iAyyjlitemService;

    @PostMapping("/info/put")
    public ResultInfo updateAdz(@RequestBody JSONObject param) {
        Adz adz = param.getObject("adz", Adz.class);
        if(!StringUtils.hasLength(adz.getDzid())){
            throw new SystemException("店长id不能为空");
        }
        iAdzService.updateById(adz);
        param.put("adz", adz);
        return ResultInfo.success(param);
    }

    @PostMapping("/info/get")
    public ResultInfo getAdz(@RequestBody JSONObject param) {
        Integer size = param.getInteger("size");
        String dzid = param.getString("dzid");
        Adz dz = iAdzService.getById(dzid);
        param.put("adz", dz);

        QueryWrapper<Amd> mdqw = new QueryWrapper<>();
        mdqw.eq("dzid", dzid);
        List<Amd> mds = iAmdService.list(mdqw);
        Set<String> mdids = new HashSet<>();
        mds.forEach(md -> {
            mdids.add(md.getMdid());
        });

        if (mdids.size() > 0) {
            QueryWrapper<Ayyjlitem> qw = new QueryWrapper<>();
            qw.in("mdid", mdids);
            Page<Ayyjlitem> page = new Page<>(1, 4);
            page = iAyyjlitemService.page(page, qw);
            param.put("yyitems", page);
        }

        return ResultInfo.success(param);
    }


    @PostMapping("/md/init")
    public ResultInfo initAdzmd(@RequestBody JSONObject param) {
        String dzid = param.getString("dzid");
        QueryWrapper<Amd> mdqw = new QueryWrapper<>();
        mdqw.eq("dzid", dzid);
        List<Amd> mds = iAmdService.list(mdqw);
        Amd amd = null;
        if (null == mds || mds.size() == 0) {
            amd = new Amd();
            amd.setMdid(StringGenerator.getID("MD"));
            amd.setCover("");
            amd.setDzid(dzid);
            amd.setImgs(new JSONArray());
            iAmdService.save(amd);
        } else {
            amd = mds.remove(0);
        }
        param.put("amd", amd);
        QueryWrapper<Afj> fjqw = new QueryWrapper<>();
        fjqw.eq("mdid", amd.getMdid());
        List<Afj> fjs = iAfjService.list(fjqw);
        param.put("fjs", fjs);

        QueryWrapper<Amj> mjqw = new QueryWrapper<>();
        mjqw.eq("mdid", amd.getMdid());
        mjqw.eq("fjid", "");
        List<Amj> mjs = iAmjService.list(mjqw);
        param.put("mjs", mjs);

        return ResultInfo.success(param);
    }
}
